package cc.catface.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * gateway的GlobalFilter全局过滤器
 */
@Component
public class CustomGlobalFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        /*System.out.println("进入" + CustomGlobalFilter.class.getSimpleName());
        String p1 = exchange.getRequest().getQueryParams().getFirst("p1");
        String p2 = exchange.getRequest().getQueryParams().getFirst("p2");
        System.out.println("p1:" + p1 + ", p2:" + p2);
        if (p1 == null) {
            exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
            return exchange.getResponse().setComplete();
        }*/
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;//过滤器的顺序，数值越小越先执行
    }
}